package com.vedu.organization.service.impl;

import com.diandian.util.BeanCopyUtil;
import com.diandian.util.MyUtil;
import com.vedu.common.PageResult;
import com.vedu.organization.constants.OrganizationConstants;
import com.vedu.organization.dao.OrganizationDao;
import com.vedu.organization.entity.bo.OrganizationBo;
import com.vedu.organization.entity.po.OrganizationPo;
import com.vedu.organization.filter.OrganizationFilter;
import com.vedu.organization.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * version: 1.0
 *
 * @author yuancheng.li [create on 2018/8/13 23:57 ]
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public boolean createOrganization(OrganizationBo organization) {

        OrganizationPo organizationPo = BeanCopyUtil.sourceToTarget(organization, OrganizationPo.class);
        int count = organizationDao.insert(organizationPo);

        return count == 1;
    }

    @Override
    public boolean modifyOrganization(OrganizationBo organization) {

        // todo

        return false;
    }

    @Override
    public boolean auditPassOrganization(Integer organizationId) {

        // param check
        MyUtil.checkNull(organizationId, "organizationId");

        OrganizationPo poDb = organizationDao.findById(organizationId);
        if (poDb == null){
            // todo throw exception
        }

        // check
        if (!(poDb.getAuditState().equals(OrganizationConstants.AUDIT_NEEDED))){
            // todo throw exception
        }

        OrganizationPo po = new OrganizationPo();
        po.setId(organizationId);
        po.setAuditState(OrganizationConstants.AUDIT_PASS);
        int count = organizationDao.update(poDb);

        return count == 1;
    }

    @Override
    public OrganizationBo findOrganizationInfo(Integer organizationId) {

        // param check
        MyUtil.checkNull(organizationId, "organizationId");

        OrganizationPo po = organizationDao.findById(organizationId);
        OrganizationBo organizationBo = BeanCopyUtil.sourceToTarget(po, OrganizationBo.class);

        return organizationBo;
    }

    @Override
    public PageResult<OrganizationBo> findByFilter(OrganizationFilter filter) {
        return null;
    }
}
